home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-06  |  5.8 KB  |  231 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        window.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __ERRORS__
  21. #include <Errors.h>
  22. #endif
  23.  
  24. #ifndef __GWLAYERS__
  25. #include <GWLayers.h>
  26. #endif
  27.  
  28. #ifndef __LOWMEM__
  29. #include <LowMem.h>
  30. #endif
  31.  
  32. #ifndef __UTILITIES__
  33. #include <Utilities.h>
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. extern short    gPrintPage;        /* Non-zero means we are printing. */
  43. extern LayerObj    gBoardLayer;
  44.  
  45.  
  46.  
  47. /*****************************************************************************/
  48. /*****************************************************************************/
  49.  
  50.  
  51.  
  52. /* This function creates a new application window.  An application window
  53. ** contains a document which is referenced by a handle in the refCon field. */
  54.  
  55. #pragma segment Window
  56. OSErr    AppNewWindow(FileRecHndl frHndl, WindowPtr *retWindow, WindowPtr behind)
  57. {
  58.     WindowPtr    oldPort, window;
  59.     OSErr        err;
  60.     Rect        rct;
  61.  
  62.     /* We will allocate our own window storage instead of letting the Window
  63.     ** Manager do it because GetNewWindow may load in temp. resources before
  64.     ** making the NewPtr call, and this can lead to heap fragmentation. */
  65.  
  66.     GetPort(&oldPort);
  67.  
  68.     err = memFullErr;        /* Assume that we will fail.  Good attitude. */
  69.  
  70.     SetRect(&rct, 0, 0, 0, 0);
  71.     window = GetStaggeredWindow(rWindow, nil, false, FrontWindow(), behind, true, rct, (long)frHndl);
  72.     if (window) {
  73.         (*frHndl)->fileState.window = window;
  74.         AppNewWindowTitle(window);
  75.         if (!(err = AppNewWindowControls(frHndl, window, behind))) {
  76.             if (gPrintPage) MoveWindow(window, 16384, 16384, true);
  77.                 /* So the window can be hidden while printing, yet
  78.                 ** PrintMonitor can get the document name. */
  79.             if ((*frHndl)->doc.justBoardWindow)
  80.                 ZoomToWindowDevice(window, rJustBoardWindowWidth, rWindowHeight, inZoomOut, true);
  81.             ShowWindow(window);
  82.             if (gPrintPage)
  83.                 MoveWindow(window, 16384, 16384, true);
  84.                     /* Moving invisible windows to the front doesn't always
  85.                     ** get them to the front.  Now that it is visible, moving
  86.                     ** it will definitely get it to the front. */
  87.         }
  88.     }
  89.  
  90.     SetPort(oldPort);
  91.     if (retWindow) *retWindow = window;
  92.  
  93.     return(err);
  94. }
  95.  
  96.  
  97.  
  98. /*****************************************************************************/
  99.  
  100.  
  101.  
  102. /* This function updates the window title to reflect the new document name.
  103. ** The new document name is stored in the fileState portion of the document.
  104. ** This is automatically set to 'Untitled # N' for new documents, and is
  105. ** updated when a user does a save-as. */
  106.  
  107. #pragma segment Window
  108. void    AppNewWindowTitle(WindowPtr window)
  109. {
  110.     FileRecHndl    frHndl;
  111.     Str255        wTitle;
  112.  
  113.     frHndl = (FileRecHndl)GetWRefCon(window);
  114.     if (frHndl) {
  115.         pcpy(wTitle, (*frHndl)->fileState.fss.name);
  116.         SetWTitle(window, wTitle);
  117.     }
  118. }
  119.  
  120.  
  121.  
  122. /*****************************************************************************/
  123.  
  124.  
  125.  
  126. /* This function returns the state of the window's document.  If the document
  127. ** is dirty, then true is returned.  If the document is clean, or the window
  128. ** has no document, then false is returned. */
  129.  
  130. #pragma segment Window
  131. Boolean    AppWindowDirty(WindowPtr window)
  132. {
  133.     FileRecHndl    frHndl;
  134.  
  135.     frHndl = (FileRecHndl)GetWRefCon(window);
  136.     if (frHndl)
  137.         return(AppDocumentDirty(frHndl));
  138.  
  139.     return(false);
  140. }
  141.  
  142.  
  143.  
  144. /*****************************************************************************/
  145.  
  146.  
  147.  
  148. /* Close all the windows.  This is called prior to quitting the application.
  149. ** This function returns true if all windows were closed.  The user may decide
  150. ** to abort a save, thus stopping the closing of the windows.  If the user
  151. ** does this, false will be returned, indicating that all windows were not
  152. ** closed after all. */
  153.  
  154. #pragma segment Window
  155. Boolean    CloseAllWindows(void)
  156. {
  157.     WindowPtr    window;
  158.  
  159.     while ((window = (WindowPtr)LMGetWindowList()) != nil) {
  160.         /* While we have a front window, try closing it. */
  161.  
  162.         if (!CloseOneWindow(window, iQuit)) return(false);
  163.             /* When CloseOneWindow returns false, this means that the window
  164.             ** didn't close.  The only cause of this is if the window had a
  165.             ** document that needed saving, and the user cancelled the save.
  166.             ** If the window succeeded in getting closed, then we are
  167.             ** returned true.  Either way, we return the result. */
  168.     }
  169.  
  170.     return(true);
  171. }
  172.  
  173.  
  174.  
  175. /*****************************************************************************/
  176.  
  177.  
  178.  
  179. /* Closes one window.  This window may be an application window, or it may be
  180. ** a system window.  If it is an application window, it may have a document
  181. ** that needs saving. */
  182.  
  183. #pragma segment Window
  184. Boolean    CloseOneWindow(WindowPtr window, short saveMode)
  185. {
  186.     FileRecHndl    frHndl;
  187.     OSErr        err;
  188.  
  189.     if (IsAppWindow(window)) {
  190.         /* First, if the window is an application window, try saving
  191.         ** the document.  Remember that the user may cancel the save. */
  192.  
  193.         frHndl = (FileRecHndl)GetWRefCon(window);
  194.         if (frHndl) {
  195.             err = AppSaveDocument(frHndl, window, saveMode);
  196.             if (err) {
  197.                 if (err != userCanceledErr)
  198.                     Alert(rErrorAlert, gAlertFilterUPP);
  199.                 return(false);
  200.             }        /* Stop closing windows on error or user cancel. */
  201.  
  202.             SetOpponentType(frHndl, kOnePlayer);
  203.             AppDisposeDocument(frHndl);
  204.                 /* The document is saved, or the user doesn't care about
  205.                 ** that document, so dispose of the document. */
  206.         }
  207.     }
  208.     DisposeAnyWindow(window);
  209.  
  210.     return(true);
  211. }
  212.  
  213.  
  214.  
  215. /*****************************************************************************/
  216.  
  217.  
  218.  
  219. #pragma segment Window
  220. WindowPtr    SetFilePort(FileRecHndl frHndl)
  221. {
  222.     WindowPtr    oldPort;
  223.  
  224.     GetPort(&oldPort);
  225.     SetPort((*frHndl)->fileState.window);
  226.     return(oldPort);
  227. }
  228.  
  229.  
  230.  
  231.